1569C - Jury Meeting - CodeForces Solution


combinatorics math *1500

Please click on ads to support us..

Python Code:

t=int(input())
mod=998244353
for asdad in range(t):
    mx=0
    mcnt=0
    n=int(input())
    liste=[-1]
    for a in input().split():
        liste.append(int(a))
        mx=max(mx,int(a))
    mcnt=liste.count(mx)
    p,b=1,1
    ans=1
    if mcnt==1:
        mcnt=liste.count(mx-1)
        p=mcnt+1
        b=mcnt
    for i in range(1,n+1):
        if(i==p):
            ans=(ans*b)%mod
        else:
            ans=(ans*i)%mod
    print(ans)

C++ Code:

#include <iostream>
#include <vector>
#include <cmath>
#include <set>
#include <random>
#include <algorithm>
#include <unordered_map>
#include <map>
using namespace std;

#define int long long

int M = 998244353;
int fact[200002] = {};
int rfact[200002] = {};


int bin_pow(int x, int n) {
    if (n == 0) return 1;
    if (n % 2) {
        return x * bin_pow(x, n-1) % M;
    } else {
        int tmp = bin_pow(x, n / 2);
        return tmp * tmp % M;
    }
}

int A(int n, int k) {
    return fact[n] * rfact[n-k] % M;
}

void solve() {
    int n;
    cin >> n;
    vector<int> arr(n);
    for (int i = 0; i < n; ++i) {
        cin >> arr[i];
    }
    sort(arr.begin(), arr.end());
    if (arr[n-1] - arr[n-2] == 0) {
        cout << fact[n] << "\n";
    } else if (arr[n-1] - arr[n-2] == 1) {
        int cnt = 1;
        for (int i = n-3; i >= 0; --i) {
            if (arr[i] == arr[i + 1]) {
                ++cnt;
            } else {
                break;
            }
        }
        int res = 0;
        for (int i = 0; i < n; ++i) {
            res += fact[n-1];
            res %= M;
            if (i >= cnt) {
                res -= A(i, cnt) * fact[n - 1 - cnt] % M;
            }
            res = (res + M) % M;
        }
        cout << res << "\n";
    } else {
        cout << 0 << "\n";
    }

}



signed main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    fact[0] = 1;
    for (int i = 1; i < 200002; ++i) {
        fact[i] = fact[i-1] * i % M;
    }
    rfact[200001] = bin_pow(fact[200001], M-2);
    for (int i = 200000; i >= 0; --i) {
        rfact[i] = rfact[i + 1] * (i + 1) % M;
    }
    int t = 1;
    cin >> t;
    while (t--) {
        solve();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1335B - Construct the String
1004B - Sonya and Exhibition
1397A - Juggling Letters
985C - Liebig's Barrels
115A - Party
746B - Decoding
1424G - Years
1663A - Who Tested
1073B - Vasya and Books
195B - After Training
455A - Boredom
1099A - Snowball
1651D - Nearest Excluded Points
599A - Patrick and Shopping
237A - Free Cash
1615B - And It's Non-Zero
1619E - MEX and Increments
34B - Sale
1436A - Reorder
1363C - Game On Leaves
1373C - Pluses and Minuses
1173B - Nauuo and Chess
318B - Strings of Power
1625A - Ancient Civilization
864A - Fair Game
1663B - Mike's Sequence
448A - Rewards
1622A - Construct a Rectangle
1620A - Equal or Not Equal
1517A - Sum of 2050